package com.rongshu.view.penpal.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.GlideUtils;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.penpal.activity.PenpalDetailActivity;
import com.rongshu.view.penpal.activity.PenpalHomeActivity;
import com.rongshu.view.penpal.bean.PenpalResponse;

import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：公共笔友会列表
 */
public class PenpalListAdapter extends RecyclerView.Adapter<PenpalListAdapter.ViewHolder> {
    private Context context;
    private List<PenpalResponse.ResponseBean> list;

    /**
     * @param context
     * @param list
     */
    public PenpalListAdapter(Context context, List<PenpalResponse.ResponseBean> list) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_pen_pal_will_item_view, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.name.setText(list.get(i).getPenclubname());
        GlideUtils.loadImageView(context, viewHolder.logo, list.get(i).getImg(),
                R.drawable.penpal_default_logo, R.drawable.penpal_default_logo);
        viewHolder.suggest.setText(list.get(i).getSignature());
        viewHolder.suggest.setVisibility(TextUtils.isEmpty(list.get(i).getSignature()) ? View.GONE : View.VISIBLE);
        String msg = "成员 " + list.get(i).getPersonnelCount() + " · " + "主题 " + list.get(i).getPostedCount();
        viewHolder.numberNum.setText(msg);
        String district = TextUtils.isEmpty(list.get(i).getDistrict()) ? "" : list.get(i).getDistrict();
        viewHolder.city.setText(" · 地区 " + district);
        if (list.get(i).getRank() == 1) {
            viewHolder.rank.setText("会长");
            viewHolder.rank.setVisibility(View.VISIBLE);
        } else {
            viewHolder.rank.setVisibility(View.GONE);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserInfoUtils.getUserInfo() == null){
                    LoginUtils.toLogin(context);
                    return;
                }
                Intent intent = new Intent();
                if (list.get(i).getIspublic() || list.get(i).getIsAdd() == 1) {//笔友会帖子公开 || 已加入该笔友会
                    intent.setClass(context, PenpalDetailActivity.class);
                    intent.putExtra(Constant.PENPAL_ID, list.get(i).getPenpalwill_id());
                    context.startActivity(intent);
                } else {
                    intent.setClass(context, PenpalHomeActivity.class);
                    intent.putExtra(Constant.PENPAL_ID, list.get(i).getPenpalwill_id());
                    context.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name, suggest, numberNum, city, rank;
        private ImageView logo;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            logo = itemView.findViewById(R.id.penpal_logo);
            name = itemView.findViewById(R.id.penpal_name);
            suggest = itemView.findViewById(R.id.penpal_suggest);
            numberNum = itemView.findViewById(R.id.penpal_member_num);
            city = itemView.findViewById(R.id.penpal_city);
            rank = itemView.findViewById(R.id.rank);
        }
    }
}
